<template>
    <div class="container-full">
      <home-header></home-header>
      <home-swiper :list="swiperlists"></home-swiper>
      <home-icons  :list="iconlist"></home-icons>
      <home-recommend :list="recommendList"></home-recommend>
      <home-weekend :list="weekendList"></home-weekend>
    </div>
</template>

<script>
import HomeHeader from './component/Header'
import HomeSwiper from './component/Swiper'
import HomeIcons from './component/Icons'
import HomeRecommend from './component/Recommend'
import HomeWeekend from './component/Weekend'
import axios from 'axios'
import { mapState } from 'vuex'

export default {
  name: 'home',
  data () {
    return {
      swiperlists: [],
      iconlist: [],
      recommendList: [],
      weekendList: []
      // lastcity: ''
    }
  },
  components: {
    HomeHeader,
    HomeSwiper,
    HomeIcons,
    HomeRecommend,
    HomeWeekend
  },
  computed: {
    // vuex提供的一个方法 等于this.$store.state.city
    ...mapState(['city'])
  },
  methods: {
    getHomeInfo () {
      // 本地请求 ，将city传给后端拿到对应数据
      // axios.get('/api/index.json?city=' + this.city)
      //   .then(this.getHomeInfoSucc)
      // gitee线上请求
      axios.get('http://linhaoj.gitee.io/vue-travel/static/mock/index.json?city=' + this.city)
        .then(this.getHomeInfoSucc)
    },
    getHomeInfoSucc (res) {
      const resjson = res.data
      if (resjson.ret && resjson.data) {
        const datas = resjson.data
        this.iconlist = datas.iconList
        this.swiperlists = datas.swiperList
        this.recommendList = datas.recommendList
        this.weekendList = datas.weekendList
      }
    }
  },
  mounted () {
    this.lastcity = this.city
    this.getHomeInfo()
  }
  // 使用<keep-alive>标签后会多出这个生命周期函数，
  // 当首页城市改变时重新发一个ajax请求
  // activated () {
  //   if (this.lastcity !== this.city) {
  //     this.getHomeInfo()
  //     this.lastcity = this.city
  //   }
  // }
}
</script>

<style scoped >
</style>
